//
//  MirrorXFilter.m
//  Core Image自定义 Filter
//
//  Created by quanhao huang on 2019/12/26.
//  Copyright © 2019 quanhao huang. All rights reserved.
//

#import "MirrorXFilter.h"

@interface MirrorXFilter()
{
    //输入图片
    CIImage  *inputImage;
}
@end

@implementation MirrorXFilter

static CIWarpKernel *customKernel = nil;

- (instancetype)init {

    self = [super init];
    if (self) {
        if (customKernel == nil)
        {
            NSBundle *bundle = [NSBundle bundleForClass: [self class]];
            NSURL *kernelURL = [bundle URLForResource:@"MirrorX" withExtension:@"cikernel"];

            NSError *error;
            NSString *kernelCode = [NSString stringWithContentsOfURL:kernelURL
                                                            encoding:NSUTF8StringEncoding error:&error];
            if (kernelCode == nil) {
                NSLog(@"Error loading kernel code string in %@\n%@",
                      NSStringFromSelector(_cmd),
                      [error localizedDescription]);
                abort();
            }

            //方法获取到真正的 CIKernel 对象
            NSArray *kernels = [CIKernel kernelsWithString:kernelCode];
            customKernel = [kernels objectAtIndex:0];
        }
    }
    return self;
}

- (CIImage *)outputImage
{
    CGFloat inputWidth = inputImage.extent.size.width;

    CIImage *result = [customKernel applyWithExtent: inputImage.extent roiCallback: ^( int index, CGRect rect ) {
        return rect;
    } inputImage: inputImage arguments: @[@(inputWidth)]];
    
    return result;
}

@end
